package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.TrainingOrganizationApply;
import com.jyy.prefabricated.service.TrainingOrganizationApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 机构注册申请
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class TrainingOrganizationApplyController {

    @Autowired
    private TrainingOrganizationApplyService applyService;

    /**
     * 新增申请
     * @param apply
     * @return
     */
    @PostMapping("/training-organization-apply")
    public AjaxResult insert(@Valid @RequestBody TrainingOrganizationApply apply){
        log.info("Rest request to insert training-organization-apply:{}",apply);
        int id = applyService.insert(apply);
        TrainingOrganizationApply result = applyService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新申请
     * @param apply
     * @return
     */
    @PutMapping("/training-organization-apply")
    public AjaxResult update(@Valid @RequestBody TrainingOrganizationApply apply){
        log.info("Rest request to update training-organization-apply:{}",apply);
        applyService.update(apply);
        TrainingOrganizationApply result = applyService.selectById(apply.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询申请详情
     * @param id
     * @return
     */
    @GetMapping("/training-organization-apply/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find training-organization-apply by id:{}",id);
        TrainingOrganizationApply result = applyService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除申请
     * @param id
     * @return
     */
    @DeleteMapping("/training-organization-apply/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete training-organization-apply by id:{}",id);
        int row = applyService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询申请信息
     * @param tel 电话
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/training-organization-apply")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String tel, @RequestParam(required = false, defaultValue = "") String startDate, @RequestParam(required = false, defaultValue = "") String endDate, Pageable pageable){
        log.info("Rest request to find training-organization-apply by params tel:{},startDate:{},endDate:{}", tel, startDate, endDate);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<TrainingOrganizationApply> apply = applyService.selectByParams(tel, startDate, endDate);
        PageInfo<TrainingOrganizationApply> result = new PageInfo<>(apply);
        return AjaxResult.success(result);
    }

    /**
     * 查询申请信息
     * @param tel 电话
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return
     */
    @GetMapping("/training-organization-apply-list")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String tel,@RequestParam(required = false, defaultValue = "") String startDate,@RequestParam(required = false, defaultValue = "") String endDate){
        log.info("Rest request to find training-organization-apply by params tel:{},startDate:{},endDate:{}", tel, startDate, endDate);
        List<TrainingOrganizationApply> result = applyService.selectByParams(tel, startDate, endDate);
        return AjaxResult.success(result);
    }

}
